from typing import Any
from urllib import request
import pandas
import pandas as pd
from pandas import DataFrame
from api.api_project import get_contract_df, create_drcs_dict
from common.mydb import SQL
from common.config import Public_Data, WebDriver_DDL_Path
from common.pd import get_tb_data
from common.playwright_driver import WebDriver
from PySide2 import QtWidgets

from common.utils import get_after_date_str
from project.web_comm import add_token_to_url
from common.obsever import Observer
from util.pd import save_df_to_excel
from api.api_file import make_file_name_by_id, remove_illegal_chars

public_data = Public_Data()


def main():
    app = QtWidgets.QApplication([])

    table = QtWidgets.QTableWidget(10, 5)  # 创建一个有10行5列的QTableWidget
    table.setHorizontalHeaderLabels(['Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'])

    # 设置列宽可调节
    table.horizontalHeader().setSectionsMovable(True)
    table.insertRow(0)
    for i in range(5):
        for j in range(5):
            table.setItem(i, j, QtWidgets.QTableWidgetItem(str(i)))

    table.show()

    # 运行应用
    app.exec_()


def get_contract():
    df_contract = get_contract_df()
    # print(df_contract)
    save_df_to_excel(df_contract,"contract_test.xlsx","contract")
    # row = df_contract.loc[1]
    # print(df_contract.loc[1]["code"])
    # row["code"] = "1"
    # print(df_contract.loc[1]["code"])


def create_drcs_flow():
    df_drcs = get_tb_data("drcs_doc", "status=0")
    drcs_dict_list = create_drcs_dict(df_drcs)
    drcs_data_dict = drcs_dict_list[0]
    return drcs_data_dict


class OB_Print(Observer):
    def update(self, msg):
        print(msg)


def remove_token(url_str: str):
    url_list = url_str.split("&")
    new_url_list = []
    for url_sub in url_list:
        if "stoken" not in url_sub:
            new_url_list.append(url_sub)
    return "&".join(new_url_list)


def down_load_file(once_id):
    url = f"http://once/once/docdownload?objectId={once_id}"
    url_str = add_token_to_url(url)

    request.urlretrieve(url_str, "down_load_data.pdf")


def sum_data(*para):
    s1, s2, *s = para
    print(s2, s)


import sys
from PySide2.QtWidgets import (QApplication, QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView)
from PySide2.QtCore import Qt

class CollapsibleTableWidget(QTableWidget):
    def __init__(self, parent=None):
        super(CollapsibleTableWidget, self).__init__(parent)
        self.row_count = 0
        self.hide_rows()

    def hide_rows(self):
        for row in range(1, self.row_count):
            self.setRowHidden(row, True)

    def setRowCount(self, row_count):
        self.row_count = row_count
        super(CollapsibleTableWidget, self).setRowCount(row_count)

    def toggle_row_visibility(self, row):
        if self.isRowHidden(row):
            self.setRowHidden(row, False)
        else:
            self.setRowHidden(row, True)

def table_main():
    app = QApplication(sys.argv)
    table = CollapsibleTableWidget()

    # Setup table
    table.setColumnCount(3)
    table.setHorizontalHeaderLabels(['Column 1', 'Column 2', 'Column 3'])
    table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)


    # Populate rows
    for row in range(5):
        for column in range(3):
            item = QTableWidgetItem(f'Item {row},{column}')
            table.setItem(row, column, item)

    # Set row count and toggle visibility
    table.setRowCount(5)
    table.toggle_row_visibility(1)  # Toggle row 1 to be visible

    table.show()
    sys.exit(app.exec_())



if __name__ == '__main__':
    # table_main()
    print(get_after_date_str())
    # url = r"http://aed.cnpdc.com/AED/Flow/DRCS/AED_DRCS_Edit.aspx?modi=edit&pk=7e804e52-cda2-4429-8edf-8064a37ba916&BIPK=7e804e52-cda2-4429-8edf-8064a37ba916&stoken=45c52f27-defa-404b-9c1f-2ad2fa512195a1"
    # print(remove_token(url))
    # print(len("AD444600010X2EA44GN-1@1-B-CFC"))
    # get_contract()

    # data_list = [1, 2, 3, 4]
    # sum_data(*data_list)

    # token = "stoken=2ed86518-d0cd-4644-b606-07a37cc1f3b3a8"
    # public_data = Public_Data()
    # public_data.Token = token

    # make_file_name_by_id("doc", 100)
    # print(remove_illegal_chars(r"dlfj?/\]lajf *332#@/"))


